import {
  systemFtpUpdateStatusById,
  systemFtpUpdateById,
  systemFtpCreate,
  systemFtpGetById,
  systemFtpPageSearchList,
  systemFtpRemoveById,
} from 'services';

/**
 * 检索-列表
 */
const defaultState = {
  /**
   * 查询表单初始数据
   */
  queryFormState: {
    status: '', // 状态
    url: '', // ftp地址
    account: '', // 账号
  },

  /**
   * 表单初始数据
   */
  ftpDataTableState: {
    page: 1, // 当前页
    limit: 10, // 页记录数
    total: 0, // 总数
    list: [] // 列表数据
  },

  /**
   * 新增表单对应的状态
   */
  addModalFormState: {
    visible: false,
  },

  /**
   * 编辑表单对应的状态
   */
  editModalFormState: {
    visible: false,
    formData: {
      address: '',
      remarks: '',
      accountNum: '',
      password: '',
      gmtCreate: '',
    },
  },

}

export default {

  namespace: 'pages_home_system_ftp',

  state: defaultState,

  effects: {
    /**
     * 新增ftp
     */
    *systemFtpCreate({ payload: { jwt, account, password, remark, url, status } }, { call, select, put }) {
      try {
        // 发起请求
        yield call(systemFtpCreate, jwt, account, password, remark, url, status);
      }
      catch (error) {
        // 如果有需要特殊处理的错误，就判断code和msg，如果没有，则继续抛出给全局处理
        // 网络异常，已经拦截掉了
        const { code, msg } = error;
        // callback错误弹框后，用户点确认时的回调,重新登录
        throw { code, msg };
      }
    },

    /**
     * 分页搜索列表
     */
    *systemFtpPageSearchList({ payload: { jwt, account, status, url, reqPage, reqLimit, } }, { call, select, put }) {
      const { data } = yield call(systemFtpPageSearchList, jwt, account, status, url, reqPage, reqLimit,);
      const { page, limit, total, list } = data;

      // 更新表单初始数据
      yield put({
        type: 'ftpDataTableStateUpdate',
        payload: { page, limit, total, list }
      });
    },
  },

  reducers: {
    /**
     * 更新表单初始数据
     */
    ftpDataTableStateUpdate(state, { payload }) {
      return { ...state, ftpDataTableState: { ...state.ftpDataTableState, ...payload } };
    },

    /**
     * 更新检索表单状态(状态要整体传入)
     */
    queryFormStateUpdate(state, { payload }) {
      const { queryFormState } = state;
      return { ...state, queryFormState: { ...queryFormState, ...payload } };
    },

    /**
     * 重置检索表单状态(状态要整体传入)
     */
    queryFormStateReset(state) {
      const { formCollapsed } = state.queryFormState;
      return { ...state, queryFormState: { ...defaultState.queryFormState, formCollapsed } }
    },

    /**
     * 更新编辑表单弹窗的状态
     */
    editModalFormStateUpdate(state, { payload }) {
      const { editModalFormState } = state;
      return { ...state, editModalFormState: { ...editModalFormState, ...payload } }
    },

    /**
     * 更新编辑表单初始值
     */
    editFormStateUpdate(state, { payload }) {
      const { editFormState } = state;
      return { ...state, editFormState: { ...editFormState, ...payload } }
    },

    /**
     * 更新新增表单状态
     */
    addModalFormStateUpdate(state, { payload }) {
      const { addModalFormState } = state;
      return { ...state, addModalFormState: { ...addModalFormState, ...payload } }
    },

    /**
     * 重置页面状态
     */
    allStateReset(state) {
      return defaultState;
    },

  },

}